# frozen_string_literal: true
# rubocop:disable Style/SignalException

PATTERNS = %w[
  %a.btn.btn-
  %button.btn.btn-
  .alert
  .alert-danger
  .alert-dismissible
  .alert-info
  .alert-link
  .alert-primary
  .alert-success
  .alert-warning
  .nav-tabs
  .toolbar-button-icon
  .tooltip
  .tooltip-inner
  <button
  <tabs
  bs-callout
  deprecated-modal
  initDeprecatedJQueryDropdown
  loading-button
  v-popover
  v-tooltip
  with_tooltip
].freeze

BLOCKING_PATTERNS = %w[
  pagination-button
  graphql_pagination
].freeze

def get_added_lines(files)
  lines = []
  files.each do |file|
    lines += helper.changed_lines(file).select { |line| %r{^[+]}.match?(line) }
  end
  lines
end

changed_vue_haml_files = helper.changed_files(/.vue$|.haml$/)

return if changed_vue_haml_files.empty?

changed_lines_in_mr = get_added_lines(changed_vue_haml_files)
deprecated_components_in_mr = PATTERNS.select { |pattern| changed_lines_in_mr.any? { |line| line[pattern] } }
blocking_components_in_mr = BLOCKING_PATTERNS.select { |pattern| changed_lines_in_mr.any? { |line| line[pattern] } }

return if (deprecated_components_in_mr + blocking_components_in_mr).empty?

markdown(<<~MARKDOWN)
  ## Deprecated components

MARKDOWN

if blocking_components_in_mr.any?
  markdown(<<~MARKDOWN)
    These deprecated components have already been migrated and can no longer be used. Please use [Pajamas components](https://design.gitlab.com/components/overview) instead.

    * #{blocking_components_in_mr.join("\n* ")}

  MARKDOWN

  fail "This merge request contains deprecated components that have been migrated and can no longer be used. Please use Pajamas components instead."
end

if deprecated_components_in_mr.any?
  markdown(<<~MARKDOWN)
    These deprecated components are in the process of being migrated. Please consider using [Pajamas components](https://design.gitlab.com/components/overview) instead.

    * #{deprecated_components_in_mr.join("\n* ")}

  MARKDOWN

  warn "This merge request contains deprecated components. Please consider using Pajamas components instead."
end
